
import { NodeType } from '@/model/node.model'
import store from '@/store'

// 并行结束节点
const AdvancedLibParallelEndNode = (graph, node) => {
  if (node.configuration) {
    const startNode = graph.getNodes().find(item => (item.data.configuration?.order === node.configuration.order) && item.data.additionalInfo.component === 'AdvancedLibParallelStartNode')
    const port = startNode.port.ports.find(item => item.group === 'out')
    const edgeSourceList = graph.getEdges().filter(item => item.source.port - 0 === port.id - 0)
    if (node.configuration.count > edgeSourceList.length) {
      node.configuration.count = edgeSourceList.length
    }
  }
}

// 服务恢复节点
const AdvancedLibServeBackNode = (graph, node) => {
  if (node.configuration) {
    const serverNodeList = graph.getNodes().filter(item => item.data.type === NodeType.serviceNode)
    const allServerList = []
    serverNodeList.forEach(item => item.data.configuration?.serviceSequence && allServerList.push(...item.data.configuration?.serviceSequence))
    const serverList = []
    // 去重
    allServerList.forEach(item => {
      if (!serverList.some(server => server.serviceKey === item.serviceKey)) {
        const { serviceKey, serviceName, metaModelId } = item
        serverList.push({ serviceKey, serviceName, metaModelId, resetStrategy: 'noReset' })
      }
    })
    // 旧列表剔除已不存在的项目
    const targetList = []
    node.configuration.serviceList.forEach(item => {
      const newItem = serverList.find(server => server.serviceKey === item.serviceKey)
      if (newItem) { targetList.push(item) }
    })
    // 新列表新增项赋值至旧列表
    serverList.forEach(item => {
      const oldItem = targetList.find(server => server.serviceKey === item.serviceKey)
      if (!oldItem) {
        targetList.push(item)
      }
    })
    // 已设置值覆盖
    node.configuration.serviceList = targetList
  }
}

// 主动触发节点
const SceneLibManualTouchNode = (graph, node) => {
  if (node.configuration) {
    node.configuration.voiceCommand = store.state.product.productInfo.voiceCommandStart
    if (store.state.product.productInfo.voiceCommandStart && !node.configuration.triggerMode.includes('voiceCommand')) {
      node.configuration.triggerMode.push('voiceCommand')
    }
  }
}

// 主动退出节点
const SceneLibManualExitNode = (graph, node) => {
  if (node.configuration) {
    node.configuration.voiceCommand = store.state.product.productInfo.voiceCommandEnd
    if (store.state.product.productInfo.voiceCommandEnd && !node.configuration.triggerMode.includes('voiceCommand')) {
      node.configuration.triggerMode.push('voiceCommand')
    }
  }
}

// 历史判断节点
const AdvancedLibHistoricalNode = (graph, node) => {
  if (node.configuration) {
    const autoTouchNodes = graph.getNodes().filter(item => item.getData().additionalInfo?.component === 'SceneLibAutoTouchNode')
    const nodeList = autoTouchNodes.map(item => {
      return {
        nodeName: item.data.name,
        nodeId: item.id,
        nodeAction: 'ignore'
      }
    })
    // 已设置值覆盖
    node.configuration.nodeList?.forEach(item => {
      const newItem = nodeList.find(node => node.nodeId === item.nodeId)
      newItem && (newItem.nodeAction = item.nodeAction)
      item.nodeAction = item.nodeAction || 'noReset'
    })
    node.configuration.nodeList = nodeList
  }
}

const formatFunc = {
  AdvancedLibParallelEndNode, // 并行结束节点
  AdvancedLibServeBackNode, // 服务恢复节点
  SceneLibManualTouchNode, // 主动触发节点
  SceneLibManualExitNode, // 主动退出节点
  AdvancedLibHistoricalNode// 历史判断节点
}

/**
   * 在校验之前将节点非法数据格式化为需要的数据格式
   * @param {object} graph 画布
   */
export default function formatSubmitData(graph) {
  graph.getNodes().forEach(item => { formatFunc[item.data.additionalInfo?.component] && formatFunc[item.data.additionalInfo.component](graph, item.data) })
}
